<?php
/**
 * Page Template
 *
 * Loaded automatically by index.php?main_page=product_info.<br />
 * Displays details of a typical product
 */
//get product listing follow product master
global $db;
$selector_string = "";
$sql_listing = "select p.products_id, pd.products_name, pd.products_short_name,
                  p.products_quantity, p.products_image,
                  p.products_price,
                  p.products_tax_class_id,
                  p.products_date_available, p.manufacturers_id,
                  p.products_weight, p.products_priced_by_attribute, p.product_is_free,
                  p.products_qty_box_status,
                  p.products_quantity_order_max,
                  p.products_discount_type, p.products_discount_type_from, p.products_sort_order, p.products_price_sorter, p.products_expiration_date,
                  p.products_dosis, pd.products_tagline, pd.products_label, pd.products_usage, pd.products_presentation, p.products_master_id
           from   " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd
           where  p.products_status = '1'
           and    (p.products_id = '" . (int)$_GET['products_id'] . "' or p.products_master_id = '" . (int)$_GET['products_id'] . "')
           and    pd.products_id = p.products_id
           and    pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
           order by p.products_price";

$product_listings = $db->Execute($sql_listing);
$rowCount = $product_listings->RecordCount();
while (!$product_listings->EOF) {
    //write product info
    ?>

    <div><?php echo '<span class="biggerText bold">' . $product_listings->fields['products_presentation'] . '</span> 
	<span class="biggerText forward">' . zen_get_products_display_price((int)$product_listings->fields['products_id']) . '</span><br /><br />'; ?> </div>
<!--bof Add to Cart Box -->
<?php
    if (CUSTOMERS_APPROVAL == 3 and TEXT_LOGIN_FOR_PRICE_BUTTON_REPLACE_SHOWROOM == '') {
  // do nothing        
    } else {
?>
    <?php
        $products_quantity = $product_listings->fields['products_quantity'];
        $products_name = $product_listings->fields['products_name'];
        $products_qty_box_status = $product_listings->fields['products_qty_box_status'];
        $products_quantity_order_max = $product_listings->fields['products_quantity_order_max'];
        $flag_show_product_info_in_cart_qty = zen_get_show_product_switch($product_listings->fields['products_id'], 'in_cart_qty');
        $title_add_to_cart = META_TAGS_BUY . " " . $product_listings->fields['products_short_name'];
        $display_qty = (($flag_show_product_info_in_cart_qty == 1 and $_SESSION['cart']->in_cart($product_listings->fields['products_id'])) ? '<p>' . PRODUCTS_ORDER_QTY_TEXT_IN_CART . $_SESSION['cart']->get_quantity($product_listings->fields['products_id']) . '</p>' : '');
        if ($products_qty_box_status == 0 or $products_quantity_order_max== 1) {
            // hide the quantity box and default to 1
            $the_button = '<input type="hidden" name="cart_quantity" value="1" />' . zen_draw_hidden_field('products_id', (int)$product_listings->fields['products_id']) . zen_image_submit(BUTTON_IMAGE_IN_CART, $title_add_to_cart, 'style="vertical-align:bottom"');//, BUTTON_IN_CART_ALT);
        } else {
              // show the quantity box
            $the_button = '<input type="text" name="cart_quantity" value="' . (zen_get_buy_now_qty($product_listings->fields['products_id'])) . '" maxlength="6" size="2" />' . zen_get_products_quantity_min_units_display((int)$product_listings->fields['products_id']) . zen_draw_hidden_field('products_id', (int)$product_listings->fields['products_id']) . zen_image_submit(BUTTON_IMAGE_IN_CART, $title_add_to_cart, 'style="vertical-align:bottom"');//, BUTTON_IN_CART_ALT);
        }
        $product_back_in_stock_notification_form_link = '';
        $display_button = zen_get_buy_now_button($product_listings->fields['products_id'], $the_button);
        
        //get the attribute option listings
        

        // limit to 1 for performance when processing larger tables
        $sql = "select count(*) as total
                from " . TABLE_PRODUCTS_OPTIONS . " popt, " . TABLE_PRODUCTS_ATTRIBUTES . " patrib
                where    patrib.products_id='" . (int)$product_listings->fields['products_id'] . "'
                    and      patrib.options_id = popt.products_options_id
                    and      popt.language_id = '" . (int)$_SESSION['languages_id'] . "'" .
                    " limit 1";


        $pr_attr = $db->Execute($sql);

        if ($pr_attr->fields['total'] > 0) {
            if (PRODUCTS_OPTIONS_SORT_ORDER=='0') {
                $options_order_by= ' order by LPAD(popt.products_options_sort_order,11,"0")';
            } else {
                $options_order_by= ' order by popt.products_options_name';
            }

            $sql = "select distinct popt.products_options_id, popt.products_options_name, popt.products_options_sort_order,
                              popt.products_options_type, popt.products_options_length, popt.products_options_comment,
                              popt.products_options_size,
                              popt.products_options_images_per_row,
                              popt.products_options_images_style,
                              popt.products_options_rows
              from        " . TABLE_PRODUCTS_OPTIONS . " popt, " . TABLE_PRODUCTS_ATTRIBUTES . " patrib
              where           patrib.products_id='" . (int)$product_listings->fields['products_id'] . "'
              and             patrib.options_id = popt.products_options_id
              and             popt.language_id = '" . (int)$_SESSION['languages_id'] . "' " .
                    $options_order_by;

            $products_options_names = $db->Execute($sql);

            if ( PRODUCTS_OPTIONS_SORT_BY_PRICE =='1' ) {
                $order_by= ' order by LPAD(pa.products_options_sort_order,11,"0"), pov.products_options_values_name';
            } else {
                $order_by= ' order by LPAD(pa.products_options_sort_order,11,"0"), pa.options_values_price';
            }


            while (!$products_options_names->EOF) {
                $products_options_array = array();                
                $sql = "select    pov.products_options_values_id,
                        pov.products_options_values_name, pa.options_id
                        FROM   " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov, " . TABLE_PRODUCTS_ATTRIBUTES . " pa LEFT JOIN " . TABLE_PRODUCTS_WITH_ATTRIBUTES_STOCK . " pwas ON pwas.stock_attributes = pa.products_attributes_id
                        WHERE   pa.products_id = '" . (int)$product_listings->fields['products_id'] . "'
                        and    pa.options_id = '" . (int)$products_options_names->fields['products_options_id'] . "'
                        and    (pwas.quantity > 0 or pwas.quantity IS NULL)
                        and    pa.options_values_id = pov.products_options_values_id
                        and       pov.language_id = '" . (int)$_SESSION['languages_id'] . "' " .
                $order_by;

                $products_options = $db->Execute($sql);
                
                while (!$products_options->EOF) {
                  
                    $products_options_array[] = array('id' => $products_options->fields['products_options_values_id'],
                                'text' => $products_options->fields['products_options_values_name']);
                    $name = $products_options->fields['options_id'];
                    $products_options->MoveNext();
                }
                $products_options_names->MoveNext();
            }
              
        }
        
  ?>
  <?php if ($products_quantity > 0) {
    // show product listing follow attribute
    
        if (sizeof($products_options_array) > 0) {
            for($i=0, $n=sizeof($products_options_array); $i<$n; $i++)
            {            
                echo '<div class="product_with_attributes">' . zen_draw_form('cart_quantity', zen_href_link(zen_get_info_page($product_listings->fields['products_id']), zen_get_all_get_params(array('action')) . 'action=add_product', $request_type), 'post', 'enctype="multipart/form-data"');
        ?>  
            <?php echo '<label>' . $products_options_array[$i]['text'] . '</label>'; ?>
            <span class="buy_button"><input type="hidden" name="id[<?php echo $name; ?>]" value="<?php echo $products_options_array[$i]['id']; ?>" /><?php echo $display_qty; echo $display_button;  ?></span>
        </form></div>
    
    <?php                    
            }
        }
        else {
        
            echo '<div class="product_with_attributes">' . zen_draw_form('cart_quantity', zen_href_link(zen_get_info_page($product_listings->fields['products_id']), zen_get_all_get_params(array('action')) . 'action=add_product', $request_type), 'post', 'enctype="multipart/form-data"');
    ?>
    
            <?php echo '<label>' . $product_listings->fields['products_name'] . '</label>'; ?>
            <span class="buy_button"><?php echo $display_qty; echo $display_button;  ?></span>
        </form></div>
        
    <?php
        }
        ?>
    <!--eof Attributes Module -->            
<?php
    }
    else if($products_quantity == 0) {
    // BEGIN CEON BACK IN STOCK NOTIFICATIONS 1 of 2  
    ?>
  
            
        <div>
            <form method="POST" action="<?php echo zen_href_link(FILENAME_BACK_IN_STOCK_NOTIFICATION_SUBSCRIBE, 'cPath=' . $_GET['cPath'] . '&products_id=' . $product_listings->fields['products_id'],'NONSSL') ?>" name="back_in_stock_notification">
                <?php 
                    $intro_text = sprintf(BACK_IN_STOCK_NOTIFICATION_TEXT_FORM_INTRO, htmlentities($products_name, ENT_COMPAT, CHARSET));
                    echo $intro_text;
                ?>
				<br><br>
                <label for="name"><?php echo BACK_IN_STOCK_NOTIFICATION_TEXT_FORM_ENTRY_NAME;?></label>
                <input type="text" size="28" maxlength="96" id="name" name="name" value="<?php echo $back_in_stock_notification_form_customer_name; ?>">
                <br class="clearBoth">
                <label for="email"><?php echo BACK_IN_STOCK_NOTIFICATION_TEXT_FORM_ENTRY_EMAIL;?></label>
                <input type="text" size="16" maxlength="96" id="email" name="email" onchange="$(this).parent().find('input[name=cofnospam]').val($(this).val())" value="<?php echo $back_in_stock_notification_form_customer_email; ?>">
                <input type="hidden" value="<?php echo $back_in_stock_notification_form_customer_email; ?>" id="hd_cofnospam" name="cofnospam" />
                <span class="forward">            
                    <?php echo '<input type="image" title=" ' . BUTTON_NOTIFY_ME_ALT . ' " alt="' . BUTTON_NOTIFY_ME_ALT 
                                . '" src="includes/templates/template_default/buttons/' . $_SESSION['language'] . '/notify_me.png" />'; ?>
				</span>		
            </form>
        </div>
<?php    
    }
    else  {
        echo '<span style="font-style: italic">' . $products_name . ' ' . TEXT_PRODUCTS_IS_DISCONTINUED . '</span>';
    }
// END CEON BACK IN STOCK NOTIFICATIONS 1 of 2
            ?>		 
<?php } // CUSTOMERS_APPROVAL == 3 ?>
<!--eof Add to Cart Box-->
    <br class="clearBoth" /><br />
<div>
<?php
    if(isset($product_listings->fields['products_master_id']))
        $selector_string .= "<option value='" . $product_listings->fields['products_dosis'] . "' selected='selected'>" . $product_listings->fields['products_name'] . "</option>";
    else
        $selector_string .= "<option value='" . $product_listings->fields['products_dosis'] . "'>" . $product_listings->fields['products_name'] . "</option>";
    $product_listings->MoveNext();
}
if(!empty($selector_string) && $rowCount > 1)
{
?>
<select id="size_selector" onchange="selector()" width="215">
    <?php echo $selector_string; ?>
</select>
<?php
}
?>